package com.tool.creatBean;

import com.core.jdbc.SqlHelper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CreateHelperOld {
    public static void main(String[] args) {
        getTableInfo(new String[]{"cust", "config"}, "E:/news/");
    }

    public static void getTableInfo(String[] tablenames, String path) {
        try {
            Connection con = SqlHelper.getConnect();
            DatabaseMetaData dbmd = con.getMetaData();
            //获取所有表名
            ResultSet tableRet = dbmd.getTables(null, null, null, null);
//            while (tableRet.next()) {
//                String tableName = (String) tableRet.getObject("TABLE_NAME");
//              // System.out.println(tableName);
//            }
            for (String tablename : tablenames) {

                //获取多个字段和类型
                ResultSet set = dbmd.getColumns(null, "%", tablename, "%");

                TableInfo tableInf = new TableInfo();
                tableInf.setTablename(tablename);
                while (set.next()) {
                    String key = set.getString("COLUMN_NAME");
                    String type = set.getString("TYPE_NAME");
                    type = type.split(" ")[0];
                    tableInf.putKeys(key, type);
                }
                //获取多个主键
                ResultSet set2 = dbmd.getPrimaryKeys(null, "%", tablename);
                while (set2.next()) {
                    String key = set2.getString("COLUMN_NAME");

                    tableInf.setId(key);
                }
                createPoFile(tableInf, path);

            }


        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public static void createPoFile(TableInfo tableInfo, String path) {
        if (path == null) path = "E:/news/";
        String filename = tableInfo.getTablename() + ".java";
        File f = new File(path + filename);

        StringBuilder str = new StringBuilder();
        StringBuilder gsstr = new StringBuilder();
        str.append("package com.package;\n");
        //生成import语句
        str.append("import java.sql.*;\n");
        str.append("import java.util.*;\n\n");
        //生成类声明语句
        str.append("public class " + upperFirstLatter(tableInfo.getTablename()) + "{\n");


        str.append("\t@MyId\n");
        GetSetInfo gsi1 = createFieldGetSet(tableInfo.getId(), toType(tableInfo.getKeys().get(tableInfo.getId())));
        str.append(gsi1.getColumn());
        gsstr.append("\n\n");
        gsstr.append(gsi1.getGetStr());
        gsstr.append("\n\n");
        gsstr.append(gsi1.getSetStr());
        gsstr.append("\n\n");

        tableInfo.getKeys().remove(tableInfo.getId());

        tableInfo.getKeys().forEach((k, v) -> {
            GetSetInfo gsi = createFieldGetSet(k, toType(tableInfo.getKeys().get(k)));
            str.append(gsi.getColumn());
            gsstr.append("\n\n");
            gsstr.append(gsi.getGetStr());
            gsstr.append("\n\n");
            gsstr.append(gsi.getSetStr());
            gsstr.append("\n\n");

        });

        str.append(gsstr.toString());

        str.append("}");
        BufferedWriter bw = null;
        try {
            //将源码写入文件
            bw = new BufferedWriter(new FileWriter(f.getAbsoluteFile()));
            bw.write(str.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    public static String toType(String column) {
        switch (column.toLowerCase()) {
            case "varchar":
            case "char":
                return "String";
            case "smallint":
            case "int":
            case "tinyint":
                return "Integer";
            case "bigint":
                return "Long";
            case "double":
                return "Double";
            case "float":
                return "Double";
            case "clob":
                return "java.sql.Clob";
            case "blob":
                return "java.sql.Blob";
            case "date":
                return "String";
            case "datetime":
                return "String";
            case "timestamp":
                return "String";
            default:
                return null;
        }

    }

    public static GetSetInfo createFieldGetSet(String key, String type) {
        GetSetInfo gsi = new GetSetInfo();
        //生成字段信息
        String cloumn = "\tprivate " + type + " " + key + ";\n";
        gsi.setColumn(cloumn);
        //生成getter
        StringBuilder getSrc = new StringBuilder();
        getSrc.append("\tpublic " + type + " get" + upperFirstLatter(key) + "(){\n");
        getSrc.append("\t\treturn " + type + ";\n");
        getSrc.append("\t}\n");
        gsi.setGetStr(getSrc.toString());
        //生成setter
        StringBuilder setSrc = new StringBuilder();
        setSrc.append("\tpublic void set" + upperFirstLatter(key) + "(" + type + " " + key + "){\n");
        setSrc.append("\t\tthis." + key + "=" + key + ";\n");
        setSrc.append("\t}\n");
        gsi.setSetStr(setSrc.toString());
        return gsi;


    }

    public static String upperFirstLatter(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

}